package com.meiqiongproperty.yiguanjia.fragment.order;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.activity.GenericFragmentActivity;
import com.meiqiongproperty.yiguanjia.databinding.OrderLifeServiceLayoutBinding;
import com.meiqiongproperty.yiguanjia.fragment.BaseFragment;
import com.meiqiongproperty.yiguanjia.fragment.home.adapter.OnClick;
import com.meiqiongproperty.yiguanjia.fragment.home.adapter.OnItemClickListener;
import com.meiqiongproperty.yiguanjia.fragment.order.orderAdapter.OrderLifeServiceListAdapter;
import com.meiqiongproperty.yiguanjia.http.api.ApiService;
import com.meiqiongproperty.yiguanjia.http.interfaces.IHttpCallback;
import com.meiqiongproperty.yiguanjia.http.sendJsonModel.SendShopOrder;
import com.meiqiongproperty.yiguanjia.httpbean.order.LifeServiceOrderBean;
import com.meiqiongproperty.yiguanjia.utils.LogUtil;
import com.meiqiongproperty.yiguanjia.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * author: Administrator
 * date: 2017/5/14.
 * package:com.meiqiongproperty.yiguanjia.fragment.order
 */
public class OrderLifeServiceFragment extends BaseFragment{
    OrderLifeServiceLayoutBinding binding;
    OrderLifeServiceListAdapter adapter;
    List<LifeServiceOrderBean.OrderlistBean> list=new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding= DataBindingUtil.inflate(inflater, R.layout.order_life_service_layout,container,false);

        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.recyclerOrderLifeService.setLayoutManager(manager);

        getLifeServiceOrderData();
        return binding.getRoot();
    }


    /**
     * 获取生活服务数据
     */
    public void getLifeServiceOrderData() {
        SendShopOrder send = new SendShopOrder();
        send.setToken(Utils.getToken(getActivity()));
        send.setRoomID(Utils.getRoomID(getActivity()));
        send.setOrderType("2");
        send.setPageNo("1");
        send.setPageSize("50");

        ApiService.getLifeServiceOrderList(getActivity(), new IHttpCallback<LifeServiceOrderBean>() {
            @Override
            public void onNext(LifeServiceOrderBean lifeServiceOrderBean) {
                Gson gson=new Gson();
                LogUtil.d("lifeService=================================>"+gson.toJson(lifeServiceOrderBean));
                list.clear();
                list.addAll(lifeServiceOrderBean.getOrderlist());
                adapter = new OrderLifeServiceListAdapter(getActivity(),list);
                binding.recyclerOrderLifeService.setAdapter(adapter);

                adapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        Bundle bundle=new Bundle();
                        bundle.putString("orderId",list.get(position).getOrderId());
                        bundle.putString("price",list.get(position).getUnitCost());
                        bundle.putString("amount",list.get(position).getServiceNum());
                        bundle.putString("serviceTime",list.get(position).getServiceTime());
                        GenericFragmentActivity.start(getActivity(),OrderLifeServiceDetailPayFragment.class,bundle);
                    }
                });
                adapter.setOnclickListener(new OnClick() {
                    @Override
                    public void onClick(View view, int position) {
                        Bundle bundle=new Bundle();
                        bundle.putString("orderId",list.get(position).getOrderId());
                        bundle.putString("price",list.get(position).getUnitCost());
                        bundle.putString("amount",list.get(position).getServiceNum());
                        bundle.putString("serviceTime",list.get(position).getServiceTime());
                        GenericFragmentActivity.start(getActivity(),OrderLifeServiceDetailPayFragment.class,bundle);
                    }
                });
            }

            @Override
            public void onError(int code, String message) {

            }
        }, Utils.getRequestBody(send));
    }
}
